﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SubMenu  {
	
	private UIToolkit _uiFont;
    private UIToolkit _uiItems;
	private UIScrollableHorizontalLayout scrollable;
	//private UITextInstance helloText;
	private UIText text;
	//private UITouchableSprite touchable;
    private UIButton touchable;
	private List<UITextInstance> lisText;
    private List<UIButton> listButton;
    public ChangedHud _chHUD;

	public UIToolkit UiFont
	{
		get { return _uiFont; }
		set {_uiFont=value;}
	}

    public UIToolkit UiItems
    {
        get { return _uiItems; }
        set { _uiItems = value; }
    }
	public SubMenu()
	{
	}
	
	public void InitializeControls()
	{
		lisText = new List<UITextInstance>();
        listButton = new List<UIButton>();
		scrollable = new UIScrollableHorizontalLayout( 10 );
		text = new UIText( UiFont, "prototype", "prototype.png" );
	}
	
	private void CreateSubMenu()
	{
		var height = UI.scaleFactor * 50f;
		var width = ScreenGeneral.pieceX / 3.8f;
		scrollable.beginUpdates();
		scrollable.setSize( width, height );
		scrollable.positionFromTopRight(0.4f,0.05f);
		scrollable.zIndex = 3;
		Element item=null;
		for(int i=0;i<ScreenGeneral.Neko.inInventory.GetSizeInventory();i++)
		{
			item = ScreenGeneral.Neko.inInventory.GetItemByIndex(i);
            //touchable = UIButton.create(UiItems,item.ImageUp, item.ImageDown, 0, 0);
            listButton.Add(UIButton.create(UiItems, item.ImageUp, item.ImageDown, 0, 0));
            listButton[i].onTouchUp += Use;
            //touchable.onTouchUp += Use;
			lisText.Add(( text.addTextInstance( item.Quantity.ToString(), 0, 0,0.5f,-1,Color.red,UITextAlignMode.Center,UITextVerticalAlignMode.Bottom )));
            lisText[i].parentUIObject = listButton[i];
			lisText[i].positionFromBottomRight(0.0f,0.0f);
            scrollable.addChild(listButton[i]);
			scrollable.endUpdates();
			scrollable.endUpdates(); 
		}
		
	}
	
	public void Show () {
		if(scrollable != null){
			CreateSubMenu();
			scrollable.hidden =false;
		}
	}
	
	public void Hide(){
		if(scrollable != null){
			scrollable.hidden = true;
			for(int i=0;i<lisText.Count;i++)
                if (lisText[i] != null)
                {
                    lisText[i].clear();
                    listButton[i].onTouchUp -= Use;
                    listButton[i].destroy();
                }
			lisText.Clear();
            listButton.Clear();
			scrollable.Clear();
		}
	}
	
	public bool GetStateShow()
	{
		return scrollable.hidden;
	}

    private Element UseItem;
    public void Use(UIButton sender)
    {
        UseItem = ScreenGeneral.Neko.inInventory.GetItemByIndex(sender.index);
        if (UseItem.Name == "WhiteBag")
        {
            ScreenGeneral.Neko.inInventory.UseElement(UseItem,1);
            ScreenGeneral.ShowMessagePickUp = true;
            ScreenGeneral.CanPickUp = true;
            Hide();
        }
    }


}
